package me.ree.zkproduct.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.ree.zkproduct.enums.ErrorCodeEnum;
import me.ree.zkproduct.enums.StatusCodeEnum;
import org.springframework.http.HttpStatus;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Resp<T> {

    private int code;

    /**
     * 兼容antd design pro的接口规范
     */
    private String status;

    private String msg;

    private T data;

    public boolean success() {
        return code >= HttpStatus.OK.value() && code < HttpStatus.MULTIPLE_CHOICES.value();
    }

    public static <T> Resp<T> badRequest(String msg) {
        return new Resp<>(StatusCodeEnum.BAD_REQUEST.getCode(), "error", msg, null);
    }

    public static <T> Resp<T> notAuth(String msg) {
        return new Resp<>(StatusCodeEnum.NOT_AUTH.getCode(), "error", msg, null);
    }

    public static <T> Resp<T> requireLogin(String msg) {
        return new Resp<>(StatusCodeEnum.REQUIRE_LOGIN.getCode(), "error", msg, null);
    }

    public static <T> Resp<T> notFound(String msg) {
        return new Resp<>(StatusCodeEnum.NOT_FOUNT.getCode(), "error", msg, null);
    }

    public static <T> Resp<T> ok(String msg, T data) {
        return new Resp<>(StatusCodeEnum.OK.getCode(), "ok", msg, data);
    }

    public static <T> Resp<T> ok(T data) {
        return ok("ok", data);
    }

    public static <T> Resp<T> conflict(String msg) {
        return new Resp<>(StatusCodeEnum.CONFLICT.getStatusCode(), "error", msg, null);
    }

    public static <T> Resp<T> fail(String msg) {
        return new Resp<>(StatusCodeEnum.FAIL.getCode(), "error", msg, null);
    }

    public static <T> Resp<T> fail(ErrorCodeEnum errorCodeEnum, String msg) {
        return new Resp<>(errorCodeEnum.getCode(), "error", msg, null);
    }
}
